package com.car.controller;

import com.car.entity.Billing;
import com.car.entity.Car;
import com.car.entity.FindVip;
import com.car.entity.Subscribe;
import com.car.service.IBillingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class BillingController {

    @Autowired
    @Qualifier("billingService")
    public IBillingService iBillingService;

    //查询订单
    @RequestMapping(value = "/findBilling")
    public String findBilling(Model model, HttpSession session){
        List<Billing> list = iBillingService.findBilling();
        session.setAttribute("all",list);
        model.addAttribute("list",list);
        return "order_List.jsp";
    }
    //删除订单
    @RequestMapping(value = "/deleteBilling")
    public String deleteBilling(int bid){
        iBillingService.deleteBilling(bid);
        iBillingService.deleteVip(bid);
        return "redirect:findBilling";
    }
    //订单详细
    @RequestMapping(value = "/findByIdBilling")
    public String findByIdBilling(int vid,Model model){
        Billing byIdBilling = iBillingService.findByIdBilling(vid);
        List<Billing> sname = iBillingService.findByIdSname(vid);
        List<Billing> pname = iBillingService.findByIdPname(vid);
        model.addAttribute("sname",sname);
        model.addAttribute("pname",pname);
        model.addAttribute("l",byIdBilling);
        return "order_Detail.jsp";
    }
    //编辑订单
    @RequestMapping(value = "/findByIdBianJi")
    public String findByIdBianJi(int vid,Model model){
        Billing byIdBilling = iBillingService.findByIdBilling(vid);
        List<Billing> sname = iBillingService.findByIdSname(vid);
        List<Billing> pname = iBillingService.findByIdPname(vid);
        model.addAttribute("sname",sname);
        model.addAttribute("pname",pname);
        model.addAttribute("l",byIdBilling);
        return "order_edit.jsp";
    }
    //删除商品服务
    @RequestMapping(value = "/deleteSnamePname")
    public String deleteSnamePname(int bid){
        iBillingService.deleteSnamePname(bid);
        return "redirect:findBilling";
    }
    //订单商品 Ename
    @RequestMapping(value = "/Ename")
    public String Ename(Model model,int vid){
        List<Billing> empEname = iBillingService.findEmpEname();
        model.addAttribute("empEname",empEname);
        System.out.println(vid);
        model.addAttribute("vid",vid);
        return "add_Ename.jsp";
    }
    //订单服务 Pname
    @RequestMapping(value = "/Pname")
    public String Pname(Model model,int vid){
        List<Billing> empPname = iBillingService.findEmpPname();
        model.addAttribute("empPname",empPname);
        System.out.println(vid);
        model.addAttribute("vid",vid);
        return "add_Pname.jsp";
    }
    //增加商品
    @RequestMapping(value = "/addSname")
    public String addSname(Billing billing){
        billing.setMoney(20);
        billing.setSmoney(30);
        iBillingService.addSname(billing);
        billing.setStar_regtime(new Date());
        iBillingService.addEmpEname(billing);
        return "redirect:findBilling";
    }
    //增加服务
    @RequestMapping(value = "/addPname")
    public String addPname(Billing billing){
        billing.setMoney(40);
        billing.setSmoney(50);
        iBillingService.addPname(billing);
        billing.setStar_regtime(new Date());
        iBillingService.addEmpEname(billing);
        return "redirect:findBilling";
    }
    //搜索订单
    @RequestMapping(value = "/findBybilling")
    public String mhcx( Model model,String regtime,String jieshu,String phone){
        List<Billing> list = iBillingService.findBybilling(regtime,jieshu,phone);
        model.addAttribute("list",list);
        return "/order_List.jsp";
    }
    //修改
    @RequestMapping(value = "/updateBilling")
    public String updateBilling(Billing billing){
        iBillingService.updateBilling(billing);
        return "redirect:findBilling";
    }



    //搜索会员信息
    @RequestMapping(value = "/findByVip")
    public String findByVip(String name,Model model){
        if (!"".equals(name) && name!=null){
            int wid = iBillingService.findByVip(name);
            List<FindVip> car = iBillingService.findCar(wid);
            List<FindVip> vip = iBillingService.findVip(wid);
            model.addAttribute("vip",vip);
            model.addAttribute("car",car);
            return "new_Order.jsp";
        }else {
            return "new_Order.jsp";
        }
    }
    //修改车辆里程
    @RequestMapping(value = "/updateMileage")
    public String updateMileage(FindVip findVip,String cid){
        System.out.println(cid);
        iBillingService.updateMileage(findVip);
        return "redirect:findVip";
    }
    //查询开单商品
    @RequestMapping(value = "/findEmpEname")
    public String findEmpEname(Model model){
        List<Billing> empEname = iBillingService.findEmpEname();
        model.addAttribute("empEname",empEname);
        return "choose_Goods.jsp";
    }
    //查询开单服务
    @RequestMapping(value = "/findEmpPname")
    public String findEmpPname(Model model){
        List<Billing> empPname = iBillingService.findEmpPname();
        model.addAttribute("empPname",empPname);
        return "choose_Serve.jsp";
    }
    //发送vid到添加爱车
    @RequestMapping(value = "/vid")
    public String vid(int userId,Model model){
        model.addAttribute("vid",userId);
        return "order_addCar.jsp";
    }
    //添加爱车
    @RequestMapping(value = "/addCar2")
    public String addCar(Car car){
        iBillingService.addCar2(car);
        return "redirect:findByVip";
    }
    //通过id查询车
    @RequestMapping(value = "/findByIdCar")
    public String findByIdCar(int cid,Model model){
        Car car = iBillingService.findByIdCar(cid);
        model.addAttribute("car",car);
        return "order_updateCar.jsp";
    }
    //编辑爱车
    @RequestMapping(value = "/updateCar")
    public String updateCar(Car car){
        iBillingService.updateCar(car);
        return "redirect:findByVip";
    }
}
